package cn.hahah.module.blog.controller.admin.userhistorylogin;

import cn.hahah.module.blog.dal.dataobject.users.UsersDO;
import cn.hahah.module.blog.service.users.UsersService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.util.object.BeanUtils;
import static cn.hahah.framework.common.pojo.CommonResult.success;

import cn.hahah.framework.excel.core.util.ExcelUtils;

import cn.hahah.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hahah.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hahah.module.blog.controller.admin.userhistorylogin.vo.*;
import cn.hahah.module.blog.dal.dataobject.userhistorylogin.UserHistoryLoginDO;
import cn.hahah.module.blog.service.userhistorylogin.UserHistoryLoginService;

@Tag(name = "管理后台 - 用户登录记录")
@RestController
@RequestMapping("/blog/user-history-login")
@Validated
public class UserHistoryLoginController {

    @Resource
    private UserHistoryLoginService userHistoryLoginService;
    @Resource
    private UsersService usersService;

    @PostMapping("/create")
    @Operation(summary = "创建用户登录记录")
    @PreAuthorize("@ss.hasPermission('blog:user-history-login:create')")
    public CommonResult<Long> createUserHistoryLogin(@Valid @RequestBody UserHistoryLoginSaveReqVO createReqVO) {
        return success(userHistoryLoginService.createUserHistoryLogin(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户登录记录")
    @PreAuthorize("@ss.hasPermission('blog:user-history-login:update')")
    public CommonResult<Boolean> updateUserHistoryLogin(@Valid @RequestBody UserHistoryLoginSaveReqVO updateReqVO) {
        userHistoryLoginService.updateUserHistoryLogin(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户登录记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:user-history-login:delete')")
    public CommonResult<Boolean> deleteUserHistoryLogin(@RequestParam("id") Long id) {
        userHistoryLoginService.deleteUserHistoryLogin(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户登录记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:user-history-login:query')")
    public CommonResult<UserHistoryLoginRespVO> getUserHistoryLogin(@RequestParam("id") Long id) {
        UserHistoryLoginDO userHistoryLogin = userHistoryLoginService.getUserHistoryLogin(id);
        return success(BeanUtils.toBean(userHistoryLogin, UserHistoryLoginRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户登录记录分页")
    @PreAuthorize("@ss.hasPermission('blog:user-history-login:query')")
    public CommonResult<PageResult<UserHistoryLoginRespVO>> getUserHistoryLoginPage(@Valid UserHistoryLoginPageReqVO pageReqVO) {
        PageResult<UserHistoryLoginDO> pageResult = userHistoryLoginService.getUserHistoryLoginPage(pageReqVO);
        PageResult<UserHistoryLoginRespVO> result = BeanUtils.toBean(pageResult, UserHistoryLoginRespVO.class);
        for (UserHistoryLoginRespVO userHistoryLoginRespVO : result.getList()) {
            UsersDO usersDO = usersService.getUsers(userHistoryLoginRespVO.getUserId());

            if(usersDO != null){
                userHistoryLoginRespVO.setUserName(usersDO.getUsername());
            }
        }

        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户登录记录 Excel")
    @PreAuthorize("@ss.hasPermission('blog:user-history-login:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserHistoryLoginExcel(@Valid UserHistoryLoginPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserHistoryLoginDO> list = userHistoryLoginService.getUserHistoryLoginPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户登录记录.xls", "数据", UserHistoryLoginRespVO.class,
                        BeanUtils.toBean(list, UserHistoryLoginRespVO.class));
    }

}